package com.tools.web.work.obs;

import com.tools.common.object.BasicEnum;
import com.tools.common.object.Note;

import java.util.Map;
import java.util.Objects;

/**
 * 华为云 OBS 对象存储服务构建客户端
 * 的必备参数项包装类对象
 * */
@Note("华为云 OBS 对象存储服务构建客户端的必备参数项包装类对象")
public class OBSSource {

    @Note("访问密钥")
    private String accessKey;

    @Note("验证密钥")
    private String secretKey;

    @Note("终端节点 URL")
    private String endPoint;

    @Note("文件需要分段上传的大小临界点，采用 OBS 的临界值：5GB")
    private long partSizeOfUpload = 1024 * 1024 * 1024 * 5L;

    /* **************************************************************************************
     *
     *          构造器
     *
     * **************************************************************************************
     * */

    public OBSSource() {}

    public static OBSSource of() {
        return new OBSSource();
    }

    @Note("读取 properties 里的配置项，转为实例返回")
    public static OBSSource from(Map properties) {
        OBSSource source = new OBSSource();
        if(properties == null || properties.isEmpty()) return source;
        source.setEndPoint((String) properties.get("obs-end-point"));
        source.setAccessKey((String) properties.get("obs-access-key"));
        source.setSecretKey((String) properties.get("obs-secret-key"));
        Object o = properties.get("obs-part-size-of-upload");
        Long uploadSize = BasicEnum.longValue(o);
        if(uploadSize != null) {
            source.setPartSizeOfUpload(uploadSize);
        }
        return source;
    }

    /* **************************************************************************************
     *
     *          Getter / Setter
     *
     * **************************************************************************************
     * */

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getEndPoint() {
        return endPoint;
    }

    public void setEndPoint(String endPoint) {
        this.endPoint = endPoint;
    }


    public long getPartSizeOfUpload() {
        return partSizeOfUpload;
    }

    public void setPartSizeOfUpload(long partSizeOfUpload) {
        this.partSizeOfUpload = partSizeOfUpload;
    }

    @Override
    public String toString() {
        return "ObsSource{\n" +
                "\n\taccessKey=" + accessKey +
                "\n\tsecretKey=" + secretKey +
                "\n\tendPoint=" + endPoint +
                "\n\tpartSizeOfUpload=" + partSizeOfUpload +
                "\n}";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OBSSource obsSource = (OBSSource) o;
        return partSizeOfUpload == obsSource.partSizeOfUpload && Objects.equals(accessKey, obsSource.accessKey) && Objects.equals(secretKey, obsSource.secretKey) && Objects.equals(endPoint, obsSource.endPoint);
    }

    @Override
    public int hashCode() {
        return Objects.hash(accessKey, secretKey, endPoint, partSizeOfUpload);
    }


    /* **************************************************************************************
     *
     *          链式调用
     *
     * **************************************************************************************
     * */

    public OBSSource accessKey(String accessKey) {
        this.setAccessKey(accessKey);
        return this;
    }

    public OBSSource secretKey(String secretKey) {
        this.setSecretKey(secretKey);
        return this;
    }

    public OBSSource endPoint(String endPoint) {
        this.setEndPoint(endPoint);
        return this;
    }

    public OBSSource partSizeOfUpload(long partSizeOfUpload) {
        this.setPartSizeOfUpload(partSizeOfUpload);
        return this;
    }
}
